package com.up.cloudfeiyibackend.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.up.cloudfeiyibackend.constant.ItemMessageConstant;
import com.up.cloudfeiyibackend.dto.*;
import com.up.cloudfeiyibackend.entity.*;
import com.up.cloudfeiyibackend.mapper.ItemMapper;
import com.up.cloudfeiyibackend.mapper.UserMapper;
import com.up.cloudfeiyibackend.result.PageResult;
import com.up.cloudfeiyibackend.result.Result;
import com.up.cloudfeiyibackend.service.ItemService;
import com.up.cloudfeiyibackend.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ItemServiceImpl implements ItemService {

    private final ItemMapper itemMapper;
    private final UserMapper userMapper;

    /**
     * 分页查询所有非遗项目
     *
     * @param itemPageDto
     * @return
     */
    @Override
    public PageResult<ItemVO> pageQuery(ItemPageDto itemPageDto) {
        PageHelper.startPage(itemPageDto.getPage(), itemPageDto.getPageSize());

        List<Item> itemList = itemMapper.pageQuery(itemPageDto);

        List<ItemVO> itemVOList = new ArrayList<>();
        for (Item item : itemList) {
            ItemVO itemVO = new ItemVO();
            BeanUtils.copyProperties(item, itemVO);
            itemVOList.add(itemVO);
        }

        Page<Item> page = (Page<Item>) itemList;
        return new PageResult<>(page.getTotal(), itemVOList);
    }

    /**
     * 获取所有非遗项目分类
     *
     * @return
     */
    @Override
    public List<ItemCategoryVO> getAllCategories() {
        List<ItemCategory> itemCategoryList = itemMapper.getAllCategories();
        List<ItemCategoryVO> itemCategoryVOList = new ArrayList<>();
        for (ItemCategory c : itemCategoryList) {
            ItemCategoryVO vo = new ItemCategoryVO();
            BeanUtils.copyProperties(c, vo);
            itemCategoryVOList.add(vo);
        }
        return itemCategoryVOList;
    }

    @Override
    public ItemDetailVO getDetailByItemId(Long itemId) {
        // 浏览量 +1
        itemMapper.increaseViewCount(itemId);

        // 获取详情
        ItemDetail itemDetail = itemMapper.getDetailByItemId(itemId);
        if (itemDetail == null){
            return null;
        }
        ItemDetailVO itemDetailVO = new ItemDetailVO();
        BeanUtils.copyProperties(itemDetail, itemDetailVO);


        // 处理图片和视频字段
        if (itemDetail.getImages() != null && !itemDetail.getImages().isEmpty()) {
            itemDetailVO.setImages(JSONUtil.toList(itemDetail.getImages(), String.class));
        }
        if (itemDetail.getVideos() != null && !itemDetail.getVideos().isEmpty()) {
            itemDetailVO.setVideos(JSONUtil.toList(itemDetail.getVideos(), String.class));
        }

        return itemDetailVO;
    }
    /**
     * 查询所有非遗项目地区
     * @return
     */
    @Override
    public Result<List<ItemRegionVO>> listRegions() {
       List<ItemRegionVO> itemRegionVOList =   itemMapper.listRegions();
        return Result.success(ItemMessageConstant.GET_REGION_SUCCESS, itemRegionVOList);
    }


    /**
     *
     * 获取该项目传承人信息
     * @param itemId
     * @return
     */
    @Override
    public Result<List<InheritorVO>> listInheritors(Long itemId) {
        List<Inheritor> inheritor =  itemMapper.getInheritor(itemId);
        List<InheritorVO> inheritorVOList = new ArrayList<>();
        for (Inheritor i : inheritor) {
            InheritorVO inheritorVO = new InheritorVO();
            String proofMaterials = i.getProofMaterials();
            List<String> list = JSONUtil.toList(proofMaterials, String.class);

            BeanUtils.copyProperties(i, inheritorVO);
            inheritorVO.setProofMaterials(list);
            inheritorVOList.add(inheritorVO);
        }
        if (inheritor == null || inheritor.isEmpty())
        {
            return Result.error("该项目无传承人信息");
        }
        return Result.success(ItemMessageConstant.GET_INHERITOR_SUCCESS,inheritorVOList );
    }
}
